import 'dart:async';
import 'package:flutter/material.dart';


class CounterHome extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    CounterBloc _counterBloc = CounterProvider.of(context).bloc;
    return Center(
      child: StreamBuilder(
        initialData: 0,
        stream: _counterBloc.Count,
        builder: (context,snapshot){
         return ActionChip(
           label: Text('${snapshot.data}'),
           onPressed:(){
             _counterBloc.Counter.add(1);
           }
         );
        },
        ),
    );
  }
}

class CounterProvider extends InheritedWidget {
   final Widget child;
   final CounterBloc bloc;
   CounterProvider({
     this.child,
     this.bloc
   }):super(child:child);

   static CounterProvider of(BuildContext context) => context.inheritFromWidgetOfExactType(CounterProvider);

   @override
  bool updateShouldNotify(InheritedWidget oldWidget) {
    // TODO: implement updateShouldNotify
    return true;
  }
  }

class CounterBloc{
  int counter = 0;
  final _counterActionController =  StreamController<int>();
  StreamSink<int> get Counter => _counterActionController.sink;
  final _counterController =  StreamController<int>();
  Stream<int> get Count => _counterController.stream;
  CounterBloc(){
    _counterActionController.stream.listen(onData);
  }
  void onData(data){
    print('$data');
    counter = data + counter;
    _counterController.add(counter);
  }
   void dispose() {
    // TODO: implement dispose
    _counterActionController.close();
    _counterController.close();
  }
  void log(){
    print('BLoC');
  }
 
}


class ConterActionButton extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    CounterBloc _counterBloc = CounterProvider.of(context).bloc;
    return FloatingActionButton(
      child: Icon(Icons.add),
      onPressed:(){
            _counterBloc.Counter.add(1);
         }
    );
  }
}